package cn.action;

import cn.base.BaseAction;
import cn.domain.*;
import cn.utils.DepartmentUtils;
import cn.utils.HQLHelper;
import cn.utils.MD5Utils;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.apache.struts2.ServletActionContext;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * Created by jiaxiang Huang on 2017/3/9.
 */
@Controller
@Scope("property")
public class UserAction extends BaseAction<User> {
    private Long parentId;
    private Long[] roleIds;

    public Long getDept() {
        return dept;
    }

    public void setDept(Long dept) {
        this.dept = dept;
    }

    private Long dept;

    public Long[] getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(Long[] roleIds) {
        this.roleIds = roleIds;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    /**
     * 查询所有数据，分页
     *
     * @return
     */
    public String findAll() {
//        List<User> list = userService.findAll();
//        getValueStack().set("list", list);
        HQLHelper hqlHelper=new HQLHelper(User.class);
        PageBean pageBean=userService.getPageBean(hqlHelper,getCurrentPage());
        List<User> list=pageBean.getRecordList();
        getValueStack().set("list",list);
        getValueStack().push(pageBean);
        return "list";
    }

    /**
     * 删除数据
     */
    public String delete() {
        userService.delete(parentId);
        return "toList";
    }

    /**
     * 初始化密码
     *
     * @return
     */
    public String initpassword() {
        User user = userService.getById(parentId);
        user.setPassword(MD5Utils.md5("1234"));
        userService.update(user);
        return "toList";
    }

    /**
     * 跳转添加页面
     *
     * @return
     */
    public String addUi() {
        model.setId(0L);
        List<Department> list = departmentService.Top();
        List<Department> treeList = DepartmentUtils.getTreeList(list, null);
        getValueStack().set("treeDept", treeList);
        List<Role> role = roleService.findAll();
        getValueStack().set("roleList", role);
        return "add";
    }

    /**
     * 修改数据，数据回显
     *
     * @return
     */
    public String edit() {
        //查询id为model id的用户
        User user = userService.getById(model.getId());
        //所有部门
        List<Department> list = departmentService.Top();
        List<Department> treeList = DepartmentUtils.getTreeList(list, null);
        getValueStack().set("treeDept", treeList);
        //查询岗位
        List<Role> role = roleService.findAll();
        getValueStack().set("roleList", role);

        //回显数据
        getValueStack().push(user);
        //判断user.getDepartment是否为空，不为空将在页面中选中部门
        if (user.getDepartment() != null) {
            dept = user.getDepartment().getId();
        }

        return "add";
    }

    /**
     * 添加用户
     *
     * @return
     */
    public String save() {
        //当前台传来的值为0时做新增操作，不为0时做修改操作
        if (model.getId() == 0) {
            if(!model.getUserName().equals("")){
                List<User> lists= userService.getUserName(model.getUserName());
                if (lists.size() > 0) {
                    model.setId(0L);
                    List<Department> list = departmentService.Top();
                    List<Department> treeList = DepartmentUtils.getTreeList(list, null);
                    getValueStack().set("treeDept", treeList);
                    List<Role> role = roleService.findAll();
                    getValueStack().set("roleList", role);
                    this.addActionError("用户名已存在");
                    return "add";
                } else {
                    model.setId(0L);
                    List<Department> list = departmentService.Top();
                    List<Department> treeList = DepartmentUtils.getTreeList(list, null);
                    getValueStack().set("treeDept", treeList);
                    List<Role> role = roleService.findAll();
                    getValueStack().set("roleList", role);
                    model.setDepartment(departmentService.getById(dept));
                    model.setRole(new HashSet<Role>(roleService.getByIds(roleIds)));
                    model.setPassword(MD5Utils.md5("1234"));
                    userService.save(model);
                    return "toList";
                }
            }else{
                model.setId(0L);
                List<Department> list = departmentService.Top();
                List<Department> treeList = DepartmentUtils.getTreeList(list, null);
                getValueStack().set("treeDept", treeList);
                List<Role> role = roleService.findAll();
                getValueStack().set("roleList", role);
                this.addActionError("用户名为空");
                return "add";
            }
        } else {
            //修改
            List<Role> roleSets = roleService.getByIds(roleIds);
            model.setRole(new HashSet<Role>(roleSets));
            User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
            model.setPassword(user.getPassword());
            Department department = departmentService.getById(dept);
            model.setDepartment(departmentService.getById(dept));
            userService.update(model);
            return "toList";
        }
    }

    /**
     * 用户登录
     *
     * @return
     */
    public String login() {
        String password = model.getPassword();
        User user = userService.getLogin(model.getUserName(), MD5Utils.md5(password));
        List<Privilege> privileges = privilegeService.privilegeTop();
        if (user != null) {
            ServletActionContext.getRequest().getSession().setAttribute("user", user);
            ServletActionContext.getRequest().getSession().setAttribute("privilegesTop", privileges);
            ServletActionContext.getRequest().getSession().setAttribute("PrivilegesFinAll", privilegeService.findAll());
            return SUCCESS;
        } else {
            addActionError("用户名或密码错误");
            return ERROR;
        }
    }

    /**
     * 退出登录
     *
     * @return
     */
    public String outLogin() {
        ServletActionContext.getRequest().getSession().removeAttribute("user");
        return "outLogin";
    }

    /**
     * 跳转修改密码
     *
     * @return
     */
    public String editPassword() {
        return "editPassword";
    }

    public String userById() {
        String userName = model.getUserName();
        List<User> list = userService.getUserName(userName);
        int flag = list.size();
        try {
            ServletActionContext.getResponse().getWriter().print(flag);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return NONE;
    }
}
